var hrefText = location.href;
    if (hrefText.indexOf("cart") != -1) {
        $("footer").find("li").eq(0).find("img").attr("src", "/static/index/img/icon_home.png")
        $("footer").find("li").eq(0).find("span").removeClass("onspan")
        $("footer").find("li").eq(1).find("img").attr("src", "/static/index/img/icon_cate.png")
        $("footer").find("li").eq(1).find("span").removeClass("onspan")
        $("footer").find("li").eq(2).find("img").attr("src", "/static/index/img/icon-information.png")
        $("footer").find("li").eq(2).find("span").removeClass("onspan")
        $("footer").find("li").eq(3).find("img").attr("src", "/static/index/img/icon_cart_selected.png")
        $("footer").find("li").eq(3).find("span").addClass("onspan")
        $("footer").find("li").eq(4).find("img").attr("src", "/static/index/img/icon_member.png")
        $("footer").find("li").eq(4).find("span").removeClass("onspan")

    }
    getCartData();

    function getCartData() {
        $.ajax({
            contentType: 'application/json',
            headers: {token: tokenk},
            //headers:{"token":window.localStorage.getItem('token'),"Content-Type":"text/plain;charset=UTF-8"},
            type: "get",
            url: Url + "cart",
            dataType: 'json',
            crossDomain: true,
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {
                showDiv()
            },
            success: function (data) {
                hiddenDiv()
                console.log(data)
                if (data.length == 0) {
                    $(".discount-main-no").css("display", "block")
                }
                var str = '';
                for (var i = 0; i < data.length; i++) {
                    var strNew = ''
                    for (var key in data[i].snapshot) {
                        strNew += '<span>' + key + ':' + data[i].snapshot[key] + '</span>&nbsp;&nbsp;'
                    }
                    console.log(strNew)
                    str += '<div class="carts-container">'
                        + '<div class="carts-item">'
                        + '<!-- 复选框图标 -->'
                        + '<input class="checkbox" type = "checkbox" _id="' + data[i].cart_id + '" name = "check"/>'
                        + '<div>'
                        + '<!-- 缩略图 -->'
                        + '<img class="carts-image" src="' + data[i].pspeci_img + '" />'
                        + '</div>'
                        + '<div class="carts-text">'
                        + '<!-- 商品标题 -->'
                        + '<span  class="carts-title">' + data[i].products_name + '</span>'
                        + '<span  class="carts-title">'
                        + strNew
                        + '</span>'
                        + '<div class="carts-subtitle">'
                        + '<!-- 价格 -->'
                        + '<span class="carts-price"><span class="carts-price_span">' + data[i].pspeci_price + '</span></span>'
                        + '<!-- 数量加减 -->'
                        + '<div class="carts-num">'
                        + '<div class="stepper">'
                        + '<!-- 减号 -->'
                        + '<span class="bindMinus"  onclick="bindMinus(this)">-</span>'
                        + '<!-- 数值 -->'
                        + '<input type="number" class="productNum" value="' + data[i].num + '" />'
                        + '<!-- 加号 -->'
                        + '<span class="normal"  onclick="bindPlus(this)">+</span>'
                        + '</div>'
                        + '</div>'
                        + '</div>'
                        + '</div>'
                        + '</div>'
                        + '<button class="delete-button" cart_id="' + data[i].cart_id + '" onclick="deleteCart(this)">删除</button>'
                        + '</div>'
                }
                $("#carts-list").html(str)
            }
        });
    }


    $("#allCheck").click(function () {
        if ($(this).attr("checked")) {
            $(".checkbox").attr("checked", true);
            var cartsItems = $(".carts-item");
            var priceNum = 0;
            for (var i = 0; i < cartsItems.length; i++) {
                var price = parseFloat(cartsItems.eq(i).find(".carts-price_span").text())
                console.log(price)
                var productNum = parseFloat(cartsItems.eq(i).find(".productNum").val())
                console.log(productNum)
                priceNum += price * productNum;
            }
            $(".total").text(priceNum.toFixed(2))
        } else {
            $(".checkbox").attr("checked", false);
            $(".total").text(0)
        }
    })
//单选框  
    $("#main").on("click", ".checkbox", function () {
        var cartsCheckboxs = $(".checkbox")
        var cartsItems = $(".carts-item");
        var priceNum = 0;
        for (var i = 0; i < cartsItems.length; i++) {
            if (cartsItems.eq(i).find(".checkbox").is(":checked")) {
                var price = parseFloat(cartsItems.eq(i).find(".carts-price_span").text())
                var productNum = parseFloat(cartsItems.eq(i).find(".productNum").val())
                priceNum += price * productNum;
            }
        }
        $(".total").text(priceNum.toFixed(2))
        for (var i = 0; i < cartsCheckboxs.length; i++) {
            if (!cartsCheckboxs.eq(i).is(":checked")) {
                $("#allCheck").attr("checked", false)
                return;
            }
        }

        $("#allCheck").attr("checked", true)

    })
    $("#main").on("input", ".productNum", function () {
        var cartsCheckboxs = $(".checkbox")
        var cartsItems = $(".carts-item");
        var priceNum = 0;
        for (var i = 0; i < cartsItems.length; i++) {
            if (cartsItems.eq(i).find(".checkbox").is(":checked")) {
                var price = parseFloat(cartsItems.eq(i).find(".carts-price_span").text())
                var productNum = parseFloat(cartsItems.eq(i).find(".productNum").val())
                priceNum += price * productNum;
            }
        }
        $(".total").text(priceNum.toFixed(2))
    })
    var endX;
    $("#main").on('touchstart', '.carts-item', function (e) {
        var touch = e.originalEvent.targetTouches[0];
        startX = touch.pageX;
        console.log(startX)
    })
    $("#main").on('touchmove', '.carts-item', function (e) {

        var touch = e.originalEvent.targetTouches[0];
        endX = (startX - touch.pageX);
        if (endX > 0 && endX <= 200) {
            $(this).css("left", -endX / 100 + 'rem')
        } else if (endX < 0) {
            $(this).css("left", 0 + 'rem')
        }
    })
    $("#main").on('touchend', '.carts-item', function (e) {
        $(".carts-item").css("left", 0 + 'rem')
        console.log(endX)
        if (endX >= 100) {
            $(this).css("left", '-2rem')
        }
    })

    function deleteCart(that) {
        $(that).parent(".carts-container").remove();
        var cart_id = $(that).attr("cart_id")
        var cartsItems = $(".carts-item");
        var priceNum = 0;
        for (var i = 0; i < cartsItems.length; i++) {
            if (cartsItems.eq(i).find(".checkbox").is(":checked")) {
                var price = parseFloat(cartsItems.eq(i).find(".carts-price_span").text())
                var productNum = parseFloat(cartsItems.eq(i).find(".productNum").val())
                priceNum += price * productNum;
            }
        }
        $(".total").text(priceNum.toFixed(2));
        $.ajax({
            type: "get",
            url: Url + 'delete/' + cart_id,
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {
                //loadData1(defaultData1)

                    showDiv()

                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                taostr("删除成功", 1000)
            },
            error: function (request, status, err) {
                failure(status)
            }
        })
    }

    function bindMinus(that) {
        var num = $(that).next("input").val();
        if (num > 1) {
            num--;
        }
        $(that).next("input").val(num)
        var cartsCheckboxs = $(".checkbox")
        var cartsItems = $(".carts-item");
        var priceNum = 0;
        for (var i = 0; i < cartsItems.length; i++) {
            if (cartsItems.eq(i).find(".checkbox").is(":checked")) {
                var price = parseFloat(cartsItems.eq(i).find(".carts-price_span").text())
                var productNum = parseFloat(cartsItems.eq(i).find(".productNum").val())
                priceNum += price * productNum;
            }
        }
        $(".total").text(priceNum.toFixed(2))
    }

    function bindPlus(that) {
        var num = $(that).prev("input").val();
        num++;
        $(that).prev("input").val(num)
        var cartsCheckboxs = $(".checkbox")
        var cartsItems = $(".carts-item");
        var priceNum = 0;
        for (var i = 0; i < cartsItems.length; i++) {
            if (cartsItems.eq(i).find(".checkbox").is(":checked")) {
                var price = parseFloat(cartsItems.eq(i).find(".carts-price_span").text())
                var productNum = parseFloat(cartsItems.eq(i).find(".productNum").val())
                priceNum += price * productNum;
            }
        }
        $(".total").text(priceNum.toFixed(2));
    }

    function getCartIds() {
        // 遍历取出已勾选的cid

        var cartIds = [];
        var nums = [];
        var obj = $('.checkbox');
        for (var i = 0; i < obj.length; i++) {
            if (obj.eq(i).is(':checked'))//选中true
            {
                cartIds.push(obj.eq(i).attr('_id'));
                nums.push(obj.eq(i).parents('.carts-container').find('.productNum').val());
            }
        }
        console.log(cartIds);
        var alls = {};
        alls['cartIds'] = cartIds;
        alls['nums'] = nums;
        return alls;
    }

    //点击立即结算
    $("#bindCheckout").on("click", function () {
        var cartIds = getCartIds();
        console.log(cartIds);
        var cartId = JSON.stringify(cartIds);
        console.log(cartId);
        storage.setItem("cartId", cartId);
        console.log(storage.getItem('cartId'));
        if (cartIds.cartIds.length <= 0) {
            taostr('请勾选商品', 1000);
            /* wx.showToast({
                 title: '请勾选商品',
                 icon: 'success',
                 duration: 1000
             })*/
        } else {
            location.href = "checkout.html";
        }
    })


